#!/bin/bash
# 清除并关闭Nginx服务
killall nginx &> /dev/null
rm -rf /usr/local/nginx
# 安装nginx所需依赖
yum -y install gcc make pcre-devel openssl-devel &> /dev/null
rm -rf /root/lnmp_soft #  清空解压释放的目录
tar xf /root/lnmp_soft.tar.gz  # 释放tar包到当前目录
cd ./lnmp_soft
tar xf nginx-1.17.6.tar.gz  # 释放Nginx到当前目录
cd nginx-1.17.6
#  编译功能
./configure  --prefix=/usr/local/nginx \
             --user=nginx  --with-http_ssl_module > /dev/null
if echo $?=0 > /dev/null;then
   make > /dev/null && make install > /dev/null  # 如果编译成功，执行安装
else 
   echo "编译失败！！" && exit  # 如果编译失败，提示并退出
fi

# 判断nginx用户是否创建
user=`awk -F : '/nginx/{print $1}' /etc/passwd`
if [ -z $user  ];then
useradd nginx -s /sbin/nologin  # 创建nginx 用户
fi

/usr/local/nginx/sbin/nginx  # 启动Nginx 服务
netstat -antulp | grep nginx /dev/null # 检测Nginx服务是否开启

if echo $? =0 > /dev/null; then
   echo -e "\033[5;32m nginx服务成功开启！\033[0m"
else
   echo -e "\033[5;31m nginx服务启动失败！\033[0m"
fi
#  设置开机自启
echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.d/rc.local
chmod +x /etc/rc.d/rc.local
if test `echo $?` = 0;then
    echo -e "\033[5;36m\n 开机自启设置成功！\033[0m"
else
    echo -e "\033[5;31m\n 开机自启设置失败！\033[0m"
fi




